package cn.cttic.base.dubbo;

import cn.cttic.base.api.RemoteBaseDeviceInfoService;
import cn.cttic.base.api.domain.BaseDeviceInfo;
import cn.cttic.base.api.domain.bo.BaseDeviceInfoBo;
import cn.cttic.base.api.domain.vo.BaseDeviceInfoVo;
import cn.cttic.base.mapper.BaseDeviceInfoMapper;
import cn.cttic.base.service.IBaseDeviceInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
@Slf4j
@RequiredArgsConstructor
public class RemoteBaseDeviceInfoServiceImpl implements RemoteBaseDeviceInfoService {

    private final IBaseDeviceInfoService baseDeviceInfoService;

    private final BaseDeviceInfoMapper baseDeviceInfoMapper;

    /**
     * 添加设备信息
     *
     * @param baseDeviceInfoBo
     * @return
     */
    @Override
    public BaseDeviceInfoVo addBaseDeviceInfo(BaseDeviceInfoBo baseDeviceInfoBo) {
        if (baseDeviceInfoService.insertByBo(baseDeviceInfoBo)) {
            return baseDeviceInfoService.queryById(baseDeviceInfoBo.getId());
        } else {
            return null;
        }
    }

    /**
     * 根据三方的设备标识查询设备信息
     *
     * @param thirdId
     * @return
     */
    @Override
    public BaseDeviceInfoVo getBaseDeviceInfoByThirdId(Long thirdId) {
        BaseDeviceInfoBo bo = new BaseDeviceInfoBo();
        bo.setThirdId(thirdId);
        List<BaseDeviceInfoVo> list = baseDeviceInfoService.queryList(bo);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    /**
     * 根据安装订单标识查询设备信息
     *
     * @param installOrderId 安装单
     * @return
     */
    @Override
    public BaseDeviceInfoVo getBaseDeviceInfoByInstallOrderId(String installOrderId) {
        List<BaseDeviceInfoVo> list =
            baseDeviceInfoMapper.selectVoList(
                new LambdaQueryWrapper<BaseDeviceInfo>()
                    .like(BaseDeviceInfo::getInstallOrderId, installOrderId)
                    .orderByDesc(BaseDeviceInfo::getCreateTime)
            );
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    /**
     * 根据IMEI查询设备标识信息
     *
     * @param imei
     * @return
     */
    @Override
    public BaseDeviceInfoVo getBaseDeviceInfoByIMEI(String imei) {
        List<BaseDeviceInfoVo> list =
            baseDeviceInfoMapper.selectVoList(
                new LambdaQueryWrapper<BaseDeviceInfo>()
                    .like(BaseDeviceInfo::getTerminalImei, imei)
                    .orderByDesc(BaseDeviceInfo::getCreateTime)
            );
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }
}
